<?php

class Empeval_Methods_Request_GetBonusGoalsForm {
	
	/**
	 *
	 * @var App_Methods_Data_App
	 */
	public $app;
	public $bonusGoalsFormId;
	
	public function __construct($params) {
		$this->app = new App_Methods_Data_App($params->app);
		$this->bonusGoalsFormId = $params->bonusGoalsFormId;
	}
	
	public function process() {
		$bonusGoalsFormAccessQuery = EmpevalViewBgfWithSuperiorPositionIdQuery::create();
		if (!Empeval_Auth::getInstance()->getUser()->isHr())
			$bonusGoalsFormAccessQuery->addAccessFilter();
		$bonusGoalsFormAccess = $bonusGoalsFormAccessQuery->filterById($this->bonusGoalsFormId)->find()->getFirst();
		if (!($bonusGoalsFormAccess instanceof EmpevalViewBgfWithSuperiorPositionId))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		$bonusGoalsFormQuery = EmpevalViewBgfWithSuperiorPositionIdQuery::create();
		$bonusGoalsFormQuery
			->useUserPositionsRelatedBySuperiorUserPositionIdQuery('SuperiorUserPosition', Criteria::LEFT_JOIN)
				->leftJoinUsers('SuperiorUser')
			->endUse()
			->filterById($this->bonusGoalsFormId)
			->leftJoinEmployeeEvaluations()
			->leftJoinSummary()
			->leftJoinBonusPeriods()
			->useRejectionsQuery(null, Criteria::LEFT_JOIN)
				->leftJoinCommittees()
			->endUse()
			->useUserPositionsRelatedByUserPositionIdQuery()
				->usePositionsQuery()
					->joinBonusGroups()
				->endUse()
				->joinUsers()
			->endUse()
			->with('UserPositionsRelatedByUserPositionId')
			->with('Users')
			->with('Positions')
			->with('BonusGroups')
			->with('EmployeeEvaluations')
			->with('Summary')
			->with('BonusPeriods')
			->with('Rejections')
			->with('Committees')
			->with('SuperiorUserPosition')
			->with('SuperiorUser');
		
		if ($bonusGoalsFormAccess->getType() == EmpevalBonusGoalsForms::LOW_GRADE_FORM) {
			$bonusGoalsFormQuery
				->leftJoinKpi()
				->useTaskGoalsQuery(null, Criteria::LEFT_JOIN)
					->leftJoinQualityCriteria()
				->endUse()
				->addJoinCondition('Kpi', 'Kpi.STATUS <> ' . EmpevalKpi::STATUS_DELETED)
				->addJoinCondition('TaskGoals', 'TaskGoals.STATUS <> ' . EmpevalTaskGoals::STATUS_DELETED)
				->with('TaskGoals')
				->with('QualityCriteria')
				->with('Kpi');
		} else {
			$bonusGoalsFormQuery
				->leftJoinMboGoals()
				->addJoinCondition('MboGoals', 'MboGoals.STATUS <> ' . EmpevalMboGoals::STATUS_DELETED)
				->with('MboGoals');
		}
		
		$bonusGoalsForm = $bonusGoalsFormQuery->find()->getFirst();
		if (!($bonusGoalsForm instanceof EmpevalViewBgfWithSuperiorPositionId))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_ACCESS_DENIED);
		
		if (!($bonusGoalsForm->getUserPositionsRelatedByUserPositionId()->getPositions()->getBonusGroups() instanceof EmpevalBonusGroups))
			throw new Empeval_Exception_App(Empeval_Exception_Codes::INFO_POSITION_DO_NOT_HAVE_ASSIGNED_BONUS_GROUP);
		
		$response = new Empeval_Methods_Response_GetBonusGoalsForm(true, $bonusGoalsForm);
		
		if ($bonusGoalsForm->getEmployeeEvaluations() != null) {
			$competanceGroups = EmpevalCompetanceGroupsQuery::create()
			->useCompetanceQuery()
				->useBehavioralIndicatorsQuery()
					->leftJoinEmployeeEvaluationValues()
					->addJoinCondition('EmployeeEvaluationValues', 'EmployeeEvaluationValues.BonusGoalsFormId = ' . $bonusGoalsForm->getId())
				->endUse()
			->endUse()
			->with('Competance')
			->with('BehavioralIndicators')
			->with('EmployeeEvaluationValues')
			->find();
			$evaluations = EmpevalEvaluationsQuery::create()->find();
			$activeCount = EmpevalBehavioralIndicatorsQuery::create()
				->useEmployeeEvaluationValuesQuery(null, Criteria::LEFT_JOIN)
					->filterByActive(1)
					->filterByBonusGoalsFormId($this->bonusGoalsFormId)
					->_or()
					->filterByActive(null)
				->endUse()
				->count();
			$response->setCompetanceGroups($competanceGroups);
			$response->setEvaluationActiveCount($activeCount);
			$response->setEvaluations($evaluations);
		}
		
		return $response;
	}
	
}